package com.amoyt.project.service.visitor.impl;

import com.amoyt.project.domain.bo.LoginUser;
import com.amoyt.project.domain.dto.insert.FeedbackAddDto;
import com.amoyt.project.domain.dto.query.FeedbackPageQuery;
import com.amoyt.project.domain.res.PageResult;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.service.visitor.FeedbackService;
import com.amoyt.project.domain.pojo.Feedback;
import com.amoyt.project.mapper.FeedbackMapper;
import com.amoyt.project.utils.ConvertBeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 意见反馈表（评论表） 服务实现类
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-21
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public Result list(FeedbackPageQuery query) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        IPage<Feedback> page = new Page<>(query.getPage(), query.getPageSize());

        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUser.getUser().getUserId());
        queryWrapper.eq(!StringUtils.isBlank(query.getFeedbackPurpose()), "feedback_purpose",query.getFeedbackPurpose());
        queryWrapper.eq(!StringUtils.isBlank(query.getIsAnonymous()), "is_anonymous",query.getIsAnonymous());

        IPage<Feedback> feedbackIPage = feedbackMapper.selectPage(page, queryWrapper);

        PageResult pageResult = PageResult.builder()
                .total(feedbackIPage.getTotal())
                .records(feedbackIPage.getRecords())
                .build();

        return Result.success(pageResult);
    }

    @Override
    public Result delete(List<Long> backIds) {
        feedbackMapper.deleteBatchIds(backIds);
        return Result.success();
    }

    @Override
    public Result detail(Long backId) {
        Feedback feedback = feedbackMapper.selectById(backId);
        return Result.success(feedback);
    }

    @Override
    public Result doFeedBack(FeedbackAddDto feedbackAddDto) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        //TODO 待完善
        Feedback feedback = ConvertBeanUtils.convert(feedbackAddDto, Feedback.class);
        feedback.setUserId(loginUser.getUser().getUserId());
        feedback.setUserName(loginUser.getUser().getUserName());
        feedback.setCreateBy(loginUser.getUser().getUserId());
        feedback.setUserAvatar(loginUser.getUser().getAvatar());
        feedback.setCreateTime(LocalDateTime.now());

        feedbackMapper.insert(feedback);

        return Result.success();
    }
}
